class Solution {
public:
    vector<vector<int>> rst;
    vector<int> temp;
    int used[6] = {0};

    vector<vector<int>> permute(vector<int>& nums) {
        permuteStep(nums);
        return rst;
    }

    void permuteStep(vector<int>& nums) {
        if(temp.size() == nums.size()) {
            rst.push_back(temp);
            return;
        }

        for(int i = 0; i < nums.size(); ++i) {
            if(used[i] == 1){
                continue;
            }

            used[i] = 1;
            temp.push_back(nums[i]);
            permuteStep(nums);
            temp.pop_back();
            used[i] = 0;
        }
    }
};